 using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;


 
namespace Defector
{
    /// <summary>
    /// Interaction logic for Notification.xaml
    /// </summary>
    public partial class Notification : Window
    {

        public SqlConnection con;

        public Notification()
        {
            InitializeComponent();

            Notify_Window.Text = "Updates have been made to existing bug(s)"; //test string

            Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
            {
                var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
                var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
                var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));

                this.Left = (corner.X - this.ActualWidth)/2;
                this.Top = (corner.Y - this.ActualHeight)/2;
            }));
        }

        private bool CheckPermission()
        {
            try
            {
                SqlClientPermission permission = new SqlClientPermission(PermissionState.Unrestricted);

                permission.Demand();

                return true;
            }
            catch
            {
                return false;
            }
        }


        private void GetTroubleList(System.Windows.Forms.ListView lview)
        {
            string proced = "uspGetTroubleInformation";

            lview.Items.Clear();

            if (!CheckPermission())
                

            SqlDependency.Stop(con.ConnectionString);
            SqlDependency.Start(con.ConnectionString);

            using (SqlCommand sqlCmd = new SqlCommand())
            {
                sqlCmd.Connection.Open();

                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                sqlCmd.CommandText = proced;

                sqlCmd.Notification = null;

                SqlDependency dependency = new SqlDependency(sqlCmd);
                dependency.OnChange += new OnChangeEventHandler(dependency_OnDataChangedDelegate);

                con.Open();

                using (SqlDataReader reader = sqlCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        System.Windows.Forms.ListViewItem lv = new System.Windows.Forms.ListViewItem();
                        lv.Text = reader.GetInt32(0).ToString();
                        lv.SubItems.Add(reader.GetString(1));
                        lv.SubItems.Add(reader.GetString(2));
                        lv.SubItems.Add(reader.GetString(3));

                        lview.Items.Add(lv);
                    }
                }
            }
        }




        

        private delegate void getTroubleListDelegate(System.Windows.Forms.ListView lv);

        private void dependency_OnDataChangedDelegate(object sender, SqlNotificationEventArgs e)
        {
            /*if (listView1.InvokeRequired)
                listView1.Invoke(new getTroubleListDelegate(GetTroubleList), listView1);
            else
                GetTroubleList(listView1);

            SqlDependency dependency = sender as SqlDependency;
            dependency.OnChange -= new OnChangeEventHandler(dependency_OnDataChangedDelegate);
             */
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}